package com.yst.suivi.services.exceptions;

import java.text.MessageFormat;
import java.util.ResourceBundle;

import javax.ejb.EJBException;

import com.yst.suivi.SuiviRessources;

public class BusinessException extends EJBException
{
	private static ResourceBundle errorBundle = null;

	static
	{
		errorBundle = ResourceBundle.getBundle("com.yst.suivi.services.exceptions.ErrorMessage");
	}

	private BusinessErrorCode codeError;

	private String[] errorParametersTranslated;

	private String[] errorParametersNotTranslated;

	public BusinessException(BusinessErrorCode _error, final String[] _errorParameters)
	{
		this(_error, _errorParameters, new String[0]);
	}

	public BusinessException(BusinessErrorCode _error, final String[] _errorParametersTranslated, final String[] _errorParametersNotTranslated)
	{
		codeError = _error;
		errorParametersTranslated = _errorParametersTranslated;
		errorParametersNotTranslated = _errorParametersNotTranslated;
	}

	public BusinessErrorCode getErrorCode()
	{
		return codeError;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Throwable#getLocalizedMessage()
	 */
	public String getLocalizedMessage()
	{
		String msg = "".intern();
		String[] errors;

		errors = new String[errorParametersTranslated.length + errorParametersNotTranslated.length];
		// translate the parameters
		for (int i = 0; i < errorParametersTranslated.length; i++)
		{
			errors[i] = SuiviRessources.getString(errorParametersTranslated[i]);
		}
		int offset = errorParametersTranslated.length;
		for (int i = 0; i < errorParametersNotTranslated.length; i++)
		{
			errors[offset + i] = errorParametersNotTranslated[i];
		}
		msg = errorBundle.getString(codeError.getCode());
		msg = MessageFormat.format(msg, errors);
		return msg;
	}
}
